import { task } from './routes/api/taskManagement.js'
import { checkWork } from './routes/api/checkWorkManagement.js'
import { feedback } from './routes/api/feedbackManagement.js'
import { employee } from './routes/api/employeeManagement.js'
import { finance } from './routes/api/financeManagement.js'
import { vertify } from './routes/api/vertify.js'
import { common } from './routes/api/common.js'
import { user } from './routes/api/user.js'
import { notice } from './routes/api/noticeManagement.js'
import { config } from './config.js'
import { export_ws } from './utils/websocket.js' //导入js文件即执行源代码，如果是函数就解析

import { createRequire } from 'module';
const require = createRequire(import.meta.url);
const fs = require('fs');

// 调用百度地图接口




/**
 * https认证
 */

const express = require('express');
const https = require('https')
let app = express();


//设置CORS
app.all('*', function (req, res, next) {
    res.header('Access-Control-Allow-Origin', "*"); //当允许携带cookies此处的白名单不能写’*’
    res.header('Access-Control-Allow-Headers', 'content-type,Content-Length, Authorization,Origin,Accept,X-Requested-With'); //允许的请求头
    res.header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT'); //允许的请求方法
    res.header('Access-Control-Allow-Credentials', true);  //允许携带cookies
    next();
});


var privateKey = fs.readFileSync('7148729_linrongtu.top.key')
var certificate = fs.readFileSync('7148729_linrongtu.top.pem')
var credentials = { key: privateKey, cert: certificate }

https.createServer(credentials, app).listen(config.serverPort);


/**
 * 启动时开启websocket的wss服务
 */
let httpsServer = https.createServer(credentials, function (req, res) {
    res.writeHead(200);
    res.end("this is a wss server!\n");
}).listen(3300);
export_ws.createWS(httpsServer);


//创建路由实例，我们可以在该实例上自由的添加路由
let mainRouter = express.Router();
let orderRouter = express.Router();

//添加两个路由到应用上

app.use('/task', task);
app.use('/checkWork', checkWork);
app.use('/feedback', feedback)
app.use('/employee', employee)
app.use('/finance', finance)
app.use('/vertify', vertify);
app.use('/user', user);
app.use('/common', common);
app.use("/notice", notice);
// 接口没响应的时候调用(error)
app.use(function (req, res) {
    res.send('error')
})

//注意这时候再加路由，就可以不带前面的/users路径了
mainRouter.get('/', function (req, res) {
    res.send('用户首页');
});

mainRouter.get('/:id', function (req, res) {
    res.send(`${req.params.id} 用户信息`);
});

//注意这时候再加路由，就可以不带前面的/order路径了
orderRouter.get('/', function (req, res) {
    res.send('订单首页');
});

orderRouter.get('/:id', function (req, res) {
    res.send(`${req.params.id} 订单信息`);
});


/**
 * 添加一个上传文件的接口
 */
